package com.fmy.dbgenerate;

import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.fmy.dbgenerate.mapper.TablesMapper;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.function.Consumer;

@SpringBootApplication
@Controller
public class DbGenerateApplication {

    public static void main(String[] args) {
        SpringApplication.run(DbGenerateApplication.class, args);
    }

    @Autowired
    private TablesMapper tablesMapper;

    @Value("${spring.datasource.url}")
    @Setter
    private String url;

    @Value("${spring.datasource.username}")
    @Setter
    private String username;

    @Value("${spring.datasource.password}")
    @Setter
    private String password;

    @Value("${spring.datasource.driver-class-name}")
    @Setter
    private String driverClassName;

    @GetMapping("/")
    public String index(Model model) {
        model.addAttribute("tables", tablesMapper.listTables());
        return "index";
    }

    @PostMapping("/generate")
    @ResponseBody
    public Boolean generate(String outputDir, String author, String moduleName, String parent, String tables) {
        try {
            common(dsc -> {
                //数据源配置
                dsc.setUrl(url);
                dsc.setDriverName(driverClassName);
                dsc.setUsername(username);
                dsc.setPassword(password);
            }, outputDir, author, moduleName, parent, tables);
        } catch (Exception e) {
            return false;
        }
        return true;
    }

    private void common(Consumer<DataSourceConfig> consumer, String outputDir, String author, String moduleName, String parent, String tables) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setOutputDir(outputDir);
        gc.setAuthor(author);
        gc.setOpen(false);
        mpg.setGlobalConfig(gc);
        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        consumer.accept(dsc);
        mpg.setDataSource(dsc);
        // 包配置
        final PackageConfig pc = new PackageConfig();
        pc.setModuleName(moduleName);
        pc.setParent(parent);
        pc.setEntity("model");//实体类包名
        mpg.setPackageInfo(pc);

        // 策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityColumnConstant(true);
        strategy.setEntityBuilderModel(true);
//        strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);
        //controller的父类
//        strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        //自定义继承的Service类全称，带包名
        strategy.setSuperServiceClass("com.dczc.core.mybatis.base.IBaseService");
        strategy.setInclude(tables.split(","));
        strategy.setControllerMappingHyphenStyle(true);
        strategy.setTablePrefix("tb_");
        mpg.setStrategy(strategy);
        mpg.execute();
    }
}
